{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## Initialize the project, set up the client and project"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b0600d1650011dc6"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "**Note:** This code utilizes functions from an older version of the Label Studio SDK (v0.0.34).\n",
    "The newer versions v1.0 and above still support the functionalities of the old version, but you will need to specify\n",
    "[`label_studio_sdk._legacy`](../../README.md) in your script."
   ],
   "id": "57cd42b243a4ea45"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# Import the SDK and the client module\n",
    "from label_studio_sdk import Client\n",
    "\n",
    "LABEL_STUDIO_URL = 'https://app.heartex.com'\n",
    "API_KEY = '<your-api-key>'\n",
    "\n",
    "# Connect to the Label Studio API and check the connection\n",
    "ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)\n",
    "ls.check_connection()\n",
    "\n",
    "# Start new project \n",
    "project = ls.start_project(\n",
    "    title='Storage Test Project',\n",
    "    label_config='''\n",
    "    <View>\n",
    "      <Image name=\"image\" value=\"$image\" zoom=\"true\"/>\n",
    "      <PolygonLabels name=\"label\" toName=\"image\">\n",
    "        <Label value=\"Airplane\" background=\"red\"/>\n",
    "      </PolygonLabels>\n",
    "    </View>\n",
    "    '''\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:27:23.341538Z",
     "start_time": "2024-03-12T02:27:21.640844Z"
    }
   },
   "id": "73a94e1a49cfe3c9",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Add a source (import) storage to the project"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "690d7251eb454b75"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import_storage = project.connect_s3_import_storage(\n",
    "    title=\"my storage\", \n",
    "    bucket=\"htx-dev\",\n",
    "    prefix=\"dataset/training_set/dogs\",\n",
    "    aws_access_key_id=\"<s3-access-key>\",\n",
    "    aws_secret_access_key=\"<s3-secret-key>\",\n",
    "    use_blob_urls=True,  # \"Treat every bucket object as a source file\", more info: https://labelstud.io/guide/storage#Treat-every-bucket-object-as-a-source-file\n",
    "    regex_filter=\".*jpg\"\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:00:05.659227Z",
     "start_time": "2024-03-12T02:00:05.002251Z"
    }
   },
   "id": "e50714816b61fc89",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'id': 17632,\n 'type': 's3',\n 'synchronizable': True,\n 'presign': True,\n 'last_sync': None,\n 'last_sync_count': None,\n 'last_sync_job': None,\n 'status': 'initialized',\n 'traceback': None,\n 'meta': {},\n 'title': 'my storage',\n 'description': '',\n 'created_at': '2024-03-12T02:00:05.404977Z',\n 'bucket': 'htx-dev',\n 'prefix': 'dataset/training_set/dogs',\n 'regex_filter': '.*jpg',\n 'use_blob_urls': True,\n 'aws_session_token': None,\n 'aws_sse_kms_key_id': None,\n 'region_name': None,\n 's3_endpoint': None,\n 'presign_ttl': 1,\n 'recursive_scan': False,\n 'project': 58689}"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import_storage"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:00:05.671880Z",
     "start_time": "2024-03-12T02:00:05.660892Z"
    }
   },
   "id": "76248c359b514c55",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Sync import storage to get tasks from the storage "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "56495cc8c60b839d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "sync_result = project.sync_import_storage(import_storage['type'], import_storage['id'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:00:06.682288Z",
     "start_time": "2024-03-12T02:00:05.674913Z"
    }
   },
   "id": "b21dfcfa1cc06d8d",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'id': 17632,\n 'type': 's3',\n 'synchronizable': True,\n 'presign': True,\n 'last_sync': None,\n 'last_sync_count': None,\n 'last_sync_job': 'f8648608-fbb5-4590-a660-9631803fd95b',\n 'status': 'queued',\n 'traceback': None,\n 'meta': {'attempts': 1, 'time_queued': '2024-03-12 02:00:06.426879+00:00'},\n 'title': 'my storage',\n 'description': '',\n 'created_at': '2024-03-12T02:00:05.404977Z',\n 'bucket': 'htx-dev',\n 'prefix': 'dataset/training_set/dogs',\n 'regex_filter': '.*jpg',\n 'use_blob_urls': True,\n 'aws_session_token': None,\n 'aws_sse_kms_key_id': None,\n 'region_name': None,\n 's3_endpoint': None,\n 'presign_ttl': 1,\n 'recursive_scan': False,\n 'project': 58689}"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sync_result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:00:06.688760Z",
     "start_time": "2024-03-12T02:00:06.683698Z"
    }
   },
   "id": "fb7aea35f6d1c361",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Get status of synchronization for the storage\n",
    "\n",
    "And wait until sync is completed. "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e8909bcce0ffe275"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage in_progress\n",
      "17632 my storage completed\n",
      "Import storage 17632 is synced!\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "while import_storage['status'] != 'completed':\n",
    "    time.sleep(3.0)  # wait for 3 seconds    \n",
    "    \n",
    "    # get all import storages and find the one we need by id\n",
    "    import_storages = project.get_import_storages()\n",
    "    for storage in import_storages:\n",
    "        if import_storage['id'] == storage['id']:\n",
    "            import_storage = storage\n",
    "            print(storage['id'], storage['title'], storage['status'])\n",
    "\n",
    "print(f'Import storage {import_storage[\"id\"]} is synced!')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:00:57.268095Z",
     "start_time": "2024-03-12T02:00:06.691848Z"
    }
   },
   "id": "26a9cc0311e30b77",
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Add a target (export) storage to the project"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8e7b78c7908bba75"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "export_storage = project.connect_s3_export_storage(\n",
    "    title=\"my export storage\", \n",
    "    bucket=\"htx-dev\",\n",
    "    prefix=\"test_out\",\n",
    "    aws_access_key_id=\"<s3-access-key>\",\n",
    "    aws_secret_access_key=\"<s3-secret-key>\",\n",
    "    can_delete_objects=False  # if you delete annotations in Label Studio, the corresponding objects in the storage will not be deleted\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:06:31.102399Z",
     "start_time": "2024-03-12T02:06:30.528712Z"
    }
   },
   "id": "2ee23267e0319077",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'id': 2975,\n 'type': 's3',\n 'synchronizable': True,\n 'last_sync': None,\n 'last_sync_count': None,\n 'last_sync_job': 'd73a8c0d-a600-47f7-b36c-2a0f112a937f',\n 'status': 'queued',\n 'traceback': None,\n 'meta': {'attempts': 1, 'time_queued': '2024-03-12 02:06:30.977353+00:00'},\n 'title': 'my export storage',\n 'description': '',\n 'created_at': '2024-03-12T02:06:30.971049Z',\n 'can_delete_objects': False,\n 'bucket': 'htx-dev',\n 'prefix': 'test_out',\n 'regex_filter': None,\n 'use_blob_urls': False,\n 'aws_session_token': None,\n 'aws_sse_kms_key_id': None,\n 'region_name': None,\n 's3_endpoint': None,\n 'project': 58690}"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "export_storage"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:06:31.837883Z",
     "start_time": "2024-03-12T02:06:31.831130Z"
    }
   },
   "id": "1f4ae7ac1e715d06",
   "execution_count": 5
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Sync target storage to save annotations to the storage\n",
    "\n",
    "Target storages sync automatically, so we don't need to call any methods to sync them. However, if you need to re-sync the storage from the scratch, you can call `sync_export_storage` method."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1b6ea2541227029d"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'id': 2975,\n 'type': 's3',\n 'synchronizable': True,\n 'last_sync': None,\n 'last_sync_count': None,\n 'last_sync_job': 'de6f4d81-3085-49b8-87ef-8ed63fbe1e62',\n 'status': 'queued',\n 'traceback': None,\n 'meta': {'attempts': 2, 'time_queued': '2024-03-12 02:06:34.927315+00:00'},\n 'title': 'my export storage',\n 'description': '',\n 'created_at': '2024-03-12T02:06:30.971049Z',\n 'can_delete_objects': False,\n 'bucket': 'htx-dev',\n 'prefix': 'test_out',\n 'regex_filter': None,\n 'use_blob_urls': False,\n 'aws_session_token': None,\n 'aws_sse_kms_key_id': None,\n 'region_name': None,\n 's3_endpoint': None,\n 'project': 58690}"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sync_result = project.sync_export_storage(export_storage['type'], export_storage['id'])\n",
    "sync_result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:06:35.057464Z",
     "start_time": "2024-03-12T02:06:34.486739Z"
    }
   },
   "id": "7e946b1b0811e327",
   "execution_count": 6
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Get status of synchronization for the target storage\n",
    "\n",
    "And wait until sync is completed."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "12069ee9fdfd9643"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2975 my export storage completed\n",
      "Export storage 2975 is synced!\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "while export_storage['status'] != 'completed':\n",
    "    time.sleep(3.0)  # wait for 3 seconds    \n",
    "    \n",
    "    # get all import storages and find the one we need by id\n",
    "    export_storages = project.get_export_storages()\n",
    "    for storage in export_storages:\n",
    "        if export_storage['id'] == storage['id']:\n",
    "            export_storage = storage\n",
    "            print(storage['id'], storage['title'], storage['status'])\n",
    "\n",
    "print(f'Export storage {export_storage[\"id\"]} is synced!')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:06:55.721024Z",
     "start_time": "2024-03-12T02:06:52.127710Z"
    }
   },
   "id": "3211ecde1d7db03e",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'id': 2975,\n 'type': 's3',\n 'synchronizable': True,\n 'last_sync': '2024-03-12T02:06:34.986049Z',\n 'last_sync_count': 0,\n 'last_sync_job': 'de6f4d81-3085-49b8-87ef-8ed63fbe1e62',\n 'status': 'completed',\n 'traceback': None,\n 'meta': {'attempts': 2,\n  'duration': 0.005824,\n  'time_queued': '2024-03-12 02:06:34.927315+00:00',\n  'time_completed': '2024-03-12 02:06:34.986054+00:00',\n  'time_last_ping': '2024-03-12 02:06:34.980230+00:00',\n  'time_in_progress': '2024-03-12 02:06:34.980230+00:00',\n  'total_annotations': 0},\n 'title': 'my export storage',\n 'description': '',\n 'created_at': '2024-03-12T02:06:30.971049Z',\n 'can_delete_objects': False,\n 'bucket': 'htx-dev',\n 'prefix': 'test_out',\n 'regex_filter': None,\n 'use_blob_urls': False,\n 'aws_session_token': None,\n 'aws_sse_kms_key_id': None,\n 'region_name': None,\n 's3_endpoint': None,\n 'project': 58690}"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "export_storage"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-12T02:07:10.776384Z",
     "start_time": "2024-03-12T02:07:10.768040Z"
    }
   },
   "id": "81f2392b7ac1a1fc",
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
